<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 30.11.11
 * Time: 13:32
 * To change this template use File | Settings | File Templates.
 */

class loginController extends Core_BaseController
{

   function  indexAction()
    {
        $auth = $this->getAuth();
        $this->twig = $this->getRegistry()->twig;


        //NEED EXTRACT TO METHOD
        $status = $auth->isAuthorizedUserVer2();
        //var_dump($status);
        if ($status != -1) {
            //Якщо користувач авторизований
            header("Location:/dzundza/www/index");

        } else {

            //якщо не авторизований
            if (isset($_POST['login']) &&
                isset($_POST['password']) &&
                !empty($_POST['login']) &&
                !empty($_POST['password'])
            ) {
                //якщо дані з форми існують
                $login = $_POST['login'];
                $password = $_POST['password'];
                if (isset($_POST['ip']))
                    $ip = $_POST['ip'];
                else $ip = 'off';

                //var_dump($_POST);
                $isUser = $auth->checkUser($login, $password);
                //var_dump($isUser);
                if ($isUser) {
                    //Якщо юзер існує в БД
                    if ($ip == 'on') {
                        $ip = $_SERVER['REMOTE_ADDR'];
                        $auth->authorizeUserVer2($login, $password, $ip);
                        //echo "authorizeUserVer2($login, $password, $ip)<br/>";

                    }
                    else {
                        $auth->authorizeUserVer2($login, $password);
                        //echo "authorizeUserVer2($login, $password)<br/>";

                    }
                    header("Location:/dzundza/www/index");
                }
                else {
                    //Не існує
                    $data = array('error' => 'Не існує такої звязки логин/пароль');
                    $template = $this->twig->loadTemplate('auth.xhtml');
                }
            } else
            {
                $data = array('error' => 'Введіть логін і пароль');
                $template = $this->twig->loadTemplate('auth.xhtml');

            }

            $template->display($data);
        }
        ////NEED EXTRACT TO METHOD
    }

    function  forgotAction()
    {
        $this->twig = $this->getRegistry()->twig;
        $dm = DataModule::getInstance();
        $auth2 = new Auth_Auth2();
        $auth2->setDataModule($dm);
        if (isset($_POST['email']) && isset($_POST['login'])) {
            $login = $_POST['login'];
            $email = $_POST['email'];
            if ($auth2->isUser($login)) {
                $newpass = $auth2->userChangePassword($login);
                echo "Your new Pass: $newpass <br/>";
                $template = $this->twig->loadTemplate('newpass.xhtml');
                $data = array('newpass' => $newpass);
                $template->display($data);

            } else {
                $template = $this->twig->loadTemplate('forgot.xhtml');
                $data = array('error' => "Пользователя не сушествует");
                $template->display($data);
            }
        } else {
            $template = $this->twig->loadTemplate('forgot.xhtml');
            $data = array('error' => "Введите логин/пароль");
            $template->display($data);
        }
    }

    function registerAction()
    {
        $dm = DataModule::getInstance();
        $auth2 = new Auth_Auth2();
        $auth2->setDataModule($dm);
        $this->twig = $this->getRegistry()->twig;

        if (isset($_POST['login']) && isset($_POST['password'])) {
            $login = $_POST['login'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            if (!$auth2->checkUser($login, $password)) {
                $auth2->registerUser($login, $password, $email);
                unset($_POST);
                $_POST = array();
                header("Location:/dzundza/www/login");
            } else {
                //no need register
                //echo incorrect login
                header("Location:/dzundza/www/index");
            }

        } else {
            $template = $this->twig->loadTemplate('register.xhtml');
            $data = array('error' => "Введите логин/пароль");
        }


        $template = $this->twig->loadTemplate('register.xhtml');
        $data = array('logged' => true);
        $template = $this->twig->loadTemplate('register.xhtml');
        $template->display($data);

    }

    function logoutAction()
    {
        $dm = DataModule::getInstance();
        $auth2 = new Auth_Auth2();
        $auth2->setDataModule($dm);
        $auth2->logout();
        header("Location:" . "/dzundza/www/index");

    }
}
